package com.rowenci.iot_agriculture.service.impl;

import com.rowenci.iot_agriculture.bean.iot.Rfid;
import com.rowenci.iot_agriculture.mapper.RfidDao;
import com.rowenci.iot_agriculture.service.api.RfidService;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.*;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 *
 * </p>
 *
 * @author rowenci
 * @date 2019/4/21 18:54
 */
@Service
public class RfidServiceImpl implements RfidService {

    @Resource
    private RfidDao rfidDao;

    @Override
    public Rfid findById(Integer id) {
        return rfidDao.findRfidById(id);
    }

    @Override
    public Rfid findByCarNumber(String rfid) {
        if (rfidDao == null){
            rfidDao = new RfidDao() {
                @Override
                public Rfid findByCarNumber(String carNumber) {
                    Rfid rfid1 = new Rfid();
                    String driver = "com.mysql.cj.jdbc.Driver";
                    String url = "jdbc:mysql://cdb-qhgx4ekn.gz.tencentcdb.com:10000/iot_agriculture?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true";
                    String username = "root";
                    String password = "bb3960519";
                    Connection conn = null;

                    try {
                        Class.forName(driver); //classLoader,加载对应驱动
                        conn = (Connection) DriverManager.getConnection(url, username, password);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }

                    ResultSet i = null;
                    String sql = "select * from rfid where car_number = '" + carNumber + "'";
                    PreparedStatement pstmt;
                    try {
                        pstmt = (PreparedStatement) conn.prepareStatement(sql);
                        i = pstmt.executeQuery();
                        while (i.next()){
                            rfid1.setId(i.getInt("id"));
                            rfid1.setCarNumber(i.getString("car_number"));
                        }
                        pstmt.close();
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    return rfid1;
                }

                @Override
                public Rfid findRfidById(Integer id) {
                    return null;
                }

                @Override
                public void updateCarNumberById(Integer id, String carNumber) {

                }

                @Override
                public List<Rfid> findAll() {
                    return null;
                }

                @Override
                public List<Rfid> findAll(Sort sort) {
                    return null;
                }

                @Override
                public List<Rfid> findAllById(Iterable<Integer> iterable) {
                    return null;
                }

                @Override
                public <S extends Rfid> List<S> saveAll(Iterable<S> iterable) {
                    return null;
                }

                @Override
                public void flush() {

                }

                @Override
                public <S extends Rfid> S saveAndFlush(S s) {
                    return null;
                }

                @Override
                public void deleteInBatch(Iterable<Rfid> iterable) {

                }

                @Override
                public void deleteAllInBatch() {

                }

                @Override
                public Rfid getOne(Integer integer) {
                    return null;
                }

                @Override
                public <S extends Rfid> List<S> findAll(Example<S> example) {
                    return null;
                }

                @Override
                public <S extends Rfid> List<S> findAll(Example<S> example, Sort sort) {
                    return null;
                }

                @Override
                public Page<Rfid> findAll(Pageable pageable) {
                    return null;
                }

                @Override
                public <S extends Rfid> S save(S s) {
                    return null;
                }

                @Override
                public Optional<Rfid> findById(Integer integer) {
                    return Optional.empty();
                }

                @Override
                public boolean existsById(Integer integer) {
                    return false;
                }

                @Override
                public long count() {
                    return 0;
                }

                @Override
                public void deleteById(Integer integer) {

                }

                @Override
                public void delete(Rfid rfid) {

                }

                @Override
                public void deleteAll(Iterable<? extends Rfid> iterable) {

                }

                @Override
                public void deleteAll() {

                }

                @Override
                public <S extends Rfid> Optional<S> findOne(Example<S> example) {
                    return Optional.empty();
                }

                @Override
                public <S extends Rfid> Page<S> findAll(Example<S> example, Pageable pageable) {
                    return null;
                }

                @Override
                public <S extends Rfid> long count(Example<S> example) {
                    return 0;
                }

                @Override
                public <S extends Rfid> boolean exists(Example<S> example) {
                    return false;
                }
            };
            return rfidDao.findByCarNumber(rfid);
        }
        Rfid rfid1 = new Rfid();
        rfid1 = rfidDao.findByCarNumber(rfid);
        return rfidDao.findByCarNumber(rfid);
    }

    @Override
    public List<Rfid> findAll() {
        return rfidDao.findAll();
    }

    @Override
    public boolean add(Rfid rfid) {
        if (rfidDao.findByCarNumber(rfid.getCarNumber()) != null){
            return false;
        }else {
            rfidDao.save(rfid);
            if (rfidDao.findByCarNumber(rfid.getCarNumber()) == null){
                return false;
            }else {
                return true;
            }
        }
    }

    @Override
    public boolean updateRfid(Rfid rfid) {
        rfidDao.updateCarNumberById(rfid.getId(), rfid.getCarNumber());
        if (rfidDao.findByCarNumber(rfid.getCarNumber()) == null){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public boolean delRfidById(Integer id) {
        rfidDao.deleteById(id);
        if (rfidDao.findRfidById(id) != null){
            return false;
        }else {
            return true;
        }
    }
}
